<?php

$fileName = $_FILES['excelFile']['name'];
$UpFilePathInfo = pathinfo($fileName);
$UpFileExt		= strtolower($UpFilePathInfo["extension"]);

$copyPath = $_SERVER["DOCUMENT_ROOT"]."/excelComplete/files/".date("YmdHis")."_".$fileName;
copy($_FILES['excelFile']['tmp_name'], $copyPath);


if($UpFileExt == 'xlsx') {
	
	ini_set('memory_limit', -1); // 메모리제한을 없엔다.
	require_once 'xlsx/Classes/PHPExcel/IOFactory.php';  // 라이브러리 파일
	$objReader = PHPExcel_IOFactory::createReader('Excel2007');
	
	$file_nm = $copyPath; // 엑셀파일 위치
	
	if( !file_exists( $file_nm ) ) {   exit(0);  }
	$worksheet_num = 1;
	
	$objReader->setReadDataOnly(true);
	
	$objPHPExcel = $objReader->load($file_nm);
	
	foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
		if( $worksheet_num > 1 ) break;
		//echo $worksheet->getTitle() . "\r\n";
		$row_num = 0;
		foreach ($worksheet->getRowIterator() as $row) {
			 
			$row_num++;
	
			if( $row_num == 1 ) continue;
			
			//echo '    - Row number: ' . $row->getRowIndex() . "\r\n";
			$cellIterator = $row->getCellIterator();
			$cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set
	
	
			foreach ($cellIterator as $cell) {
	
				$cell_value = "";
				$cell_coordi = "";
	
				if (!is_null($cell)) {
					$cell_value = trim( $cell->getCalculatedValue() );
					$cell_coordi = $cell->getCoordinate();
				}
	
				echo iconv('utf-8', 'euc-kr', $cell_value) . "<BR>";
	
			}
	
		}
		$worksheet_num++;
	}
	
	
} else if(stristr($fileName, ".xls")) {
	echo 'xls 파일<br>';
	
	require_once 'xls/reader.php';
	
	$data = new Spreadsheet_Excel_Reader();
	$data->setOutputEncoding('CP949');
	$data->read($copyPath);
	
	error_reporting(E_ALL ^ E_NOTICE);
	
	for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) {
		for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
			echo "\"".$data->sheets[0]['cells'][$i][$j]."\",";
		}
		echo "\n";
	}
	
} else {
	echo '지원하지 않는 파일';
}
?>

<form name="frm" method="post" action="index.php" enctype="multipart/form-data">
<input type="file" name="excelFile" />
<input type="submit" />
</form>
